import { Controller } from "../../mvc/core/Controller";
import { IConsumeCountGiftBagController } from "./interfaces/IConsumeCountGiftBagController";
import { ConsumeCountIcon } from "../view/consumeCount/ConsumeCountIcon";
import { EventName } from "../mvc/EventName";
import { ConsumeEverydayIcon } from "../view/consumeCount/ConsumeEverydayIcon";
import { IConsumeEverydayModule } from "../../../modules/interfaces/IConsumeEverydayModule";
import { ConsumeEverydayModule } from "../../../modules/ConsumeEverydayModule";
import { IView } from "../../mvc/interfaces/IView";
import { ConsumeCountGiftBagModule } from "../../../modules/ConsumeCountGiftBagModule";
import { GModuleEvent } from "../mvc/GModuleEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ConsumeCountData } from "../view/consumeCount/ConsumeCountData";
import { ClockManager } from "../manager/ClockManager";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { TOpenRechargeGiftBag } from "../../../Message/Db/Tables/TOpenRechargeGiftBag";
import { SSeqConsumeYBRetRewardInfo } from "../../../Message/Public/SSeqConsumeYBRetRewardInfo";
import { IConsumeCountGiftBagModule } from "../../../modules/interfaces/IConsumeCountGiftBagModule";
type int = number;
//class ConsumeCountGiftBagController
    
    export  class ConsumeCountGiftBagController extends Controller implements IConsumeCountGiftBagController
    {
       

        public get consumeCountIcon(): ConsumeCountIcon
        {
            if (this._consumeCountIcon == null) 
            {
                this._consumeCountIcon = new ConsumeCountIcon();
                this._consumeCountIcon.updateIconInfo();
                this._consumeCountIcon.addEventListener(EventName.ConsumeCountIconClick, this.onConsumeCountIconClickHandler);
            }
            return this._consumeCountIcon;
        }

        public get consumeEverydayIcon(): ConsumeEverydayIcon
        {
            if (this._consumeEverydayIcon == null) 
            {
                this._consumeEverydayIcon = new ConsumeEverydayIcon();
                this._consumeEverydayIcon.updateIconInfo();
            }
            return this._consumeEverydayIcon;
        }

        public get consumeEverydayModule(): IConsumeEverydayModule
        {
            if (this._consumeEverydayModule == null) 
            {
                this._consumeEverydayModule = new ConsumeEverydayModule();
            }
            return this._consumeEverydayModule;
        }

        protected /* override */ initView(): IView
        {
            if (this._consumeCount == null) 
            {
                this._consumeCount = new ConsumeCountGiftBagModule();
                this._consumeCount.type = GModuleEvent.Type_ConsumeCount;
            }
            return this._consumeCount;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.ConsumeCountActiveCommand, this.onConsumeCountActiveHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateActiveConsumeCount, this.onActiveConsumeCountHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveListChange, this.onActiveListChangeHandler);
            Dispatcher.addEventListener(EventName.ConsumeCountGetCodeSuccess, this.onGetCodeSuccessHandler);
            Dispatcher.addEventListener(EventName.ConsumeCountItemSelected, this.onConsumeCountItemSelectedHandler);
            Dispatcher.addEventListener(EventName.ConsumeCountGetGiftBag, this.onConsumeCountGetGiftBagHandler);
            Dispatcher.addEventListener(EventName.ConsumeIsGetGift, this.onGiftIsCanGet);
            NetDispatcher.addCmdListener(ServerCommand.ConsumeYBRetRewardActive, this.onConsumeYBRetRewardActiveHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateConsumeYBRetEveryday, this.onConsumeYBRetEverydayHandler);
            Dispatcher.addEventListener(EventName.ConsumeEverydayGetReward, this.onConsumeEverydayGetRewardHandler);
            Dispatcher.addEventListener(EventName.ConsumeEverydayIconClick, this.onConsumeEverydayIconClickHandler);
            return;
        }

        /* internal  */onGiftIsCanGet(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (loc1 == 1 || loc1 == 2 || loc1 == 4) 
            {
                this.consumeCountIcon.setFlashEffect(true);
            }
            return;
        }

        /* internal  */onConsumeCountActiveHandler(arg1: SActiveToClient): void
        {
            var loc1=cache.active.isActiveInEffect(arg1.code);
            if (loc1) 
            {
                ConsumeCountData.activeToClient = arg1;
                ClockManager.instance.addDateCall(arg1.endDt, this.removeConsumeCountActive);
                if (ConsumeCountData.isRechargeActive && !ConsumeCountData.isHadGetAllGift(arg1.code)) 
                {
                    this.consumeCountIcon.show();
                }
                if (this._consumeCount) 
                {
                    this._consumeCount.type = GModuleEvent.Type_ConsumeCount;
                }
            }
            if (loc1 && !this._isGetCodes) 
            {
                GameProxy.roleProxy.hadGetConsumeCountGiftBag();
                this._isGetCodes = true;
            }
            return;
        }

        /* internal  */removeConsumeCountActive(): void
        {
            if (this._consumeCountIcon && !this._consumeCountIcon.isHide) 
            {
                this.consumeCountIcon.hide();
            }
            if (this._consumeCount && !this._consumeCount.isHide) 
            {
                view.hide();
            }
            if (ConsumeCountData.activeToClient) 
            {
                ClockManager.instance.removeDateCall(ConsumeCountData.activeToClient.endDt);
            }
            this._isGetCodes = false;
            return;
        }

        /* internal  */onActiveConsumeCountHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            cache.role.consumeCount = loc1.value;
            cache.role.consumeWorldLevle = parseInt(loc1.valueStr);
            if (this._consumeCount) 
            {
                this._consumeCount.updateGiftArr();
                this._consumeCount.updateGiftBag();
                this._consumeCount.updateConsumeCountGold(loc1.value);
            }
            if (ConsumeCountData.isCanGetGift() && this.hadGetArr) 
            {
                this.consumeCountIcon.setFlashEffect(true);
            }
            return;
        }

        /* internal  */onActiveListChangeHandler(arg1: Object): void
        {
            var loc1=cache.active.isActiveInEffect(EActiveType._EActiveTypeConsumeCount);
            var loc2=cache.active.isActiveInEffect(EActiveType._EActiveTypeConsumeCountTwo);
            var loc3=cache.active.isActiveInEffect(EActiveType._EActiveTypeConsumeCountThree);
            if (!loc1 && !loc2 && !loc3) 
            {
                this.removeConsumeCountActive();
            }
            this.isGetAllGift();
            var loc4;
            if (!(loc4 = cache.active.isActiveInEffect(EActiveType._EActiveTypeConsumeYBRetReward))) 
            {
                if (this._consumeEverydayIcon) 
                {
                    this.consumeEverydayIcon.hide();
                }
                if (this._consumeEverydayModule) 
                {
                    this._consumeEverydayModule.hide();
                }
            }
            return;
        }

        /* internal  */onConsumeCountIconClickHandler(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
                this._consumeCount.type = GModuleEvent.Type_ConsumeCount;
                GameProxy.roleProxy.hadGetConsumeCountGiftBag();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */isGetAllGift(): void
        {
            var loc1=ConsumeCountData.isHadGetAllGift(EActiveType._EActiveTypeConsumeCount);
            var loc2=ConsumeCountData.isHadGetAllGift(EActiveType._EActiveTypeConsumeCountTwo);
            var loc3=ConsumeCountData.isHadGetAllGift(EActiveType._EActiveTypeConsumeCountThree);
            if (loc1 && loc2 && loc3) 
            {
                this.removeConsumeCountActive();
            }
            return;
        }

        /* internal  */onGetCodeSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            ConsumeCountData.hadGetCodes = loc1;
            this.hadGetArr = loc1;
            if (this._consumeCount) 
            {
                this._consumeCount.updateGiftBag();
                this._consumeCount.updateConsumeCountGold(cache.role.consumeCount);
            }
            this.isGetAllGift();
            if (ConsumeCountData.isCanGetGift()) 
            {
                this.consumeCountIcon.setFlashEffect(true);
            }
            else 
            {
                this.consumeCountIcon.setFlashEffect(false);
            }
            return;
        }

        /* internal  */onConsumeCountItemSelectedHandler(arg1: DataEvent): void
        {
            if (this._consumeCount) 
            {
                this._consumeCount.updateItemList(arg1.data as TOpenRechargeGiftBag);
            }
            return;
        }

        /* internal  */onConsumeCountGetGiftBagHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.getConsumeCountGiftBag(loc1);
            this._consumeCountIcon.setFlashEffect(false);
            return;
        }

        /* internal  */onConsumeYBRetRewardActiveHandler(arg1: Object): void
        {
            if (!(ConsumeCountData.everydayRewardInfos == null) && ConsumeCountData.everydayRewardInfos.length > 0) 
            {
                this.consumeEverydayIcon.show();
            }
            return;
        }

        /* internal  */onConsumeYBRetEverydayHandler(arg1: MessageBlock): void
        {
            var loc3=null;
            var loc4=0;
            var loc5=0;
            var loc1=arg1.messageBase as SSeqConsumeYBRetRewardInfo;
            ConsumeCountData.everydayConsumeGold = loc1.dayCost;
            ConsumeCountData.everydayRewardInfos = loc1.rewardInfos;
            var loc2=cache.active.isActiveInEffect(EActiveType._EActiveTypeConsumeYBRetReward);
            if (loc2 && !(ConsumeCountData.everydayRewardInfos == null) && ConsumeCountData.everydayRewardInfos.length > 0) 
            {
                this.consumeEverydayIcon.show();
                if (this._consumeEverydayModule && !this._consumeEverydayModule.isHide) 
                {
                    this._consumeEverydayModule.updateModule();
                }
                loc4 = loc1.rewardInfos.length;
                loc5 = 0;
                while (loc5 < loc4) 
                {
                    if ((loc3 = loc1.rewardInfos[loc5]) && loc1.dayCost >= loc3.target) 
                    {
                        this.consumeEverydayIcon.setFlashEffect(true);
                    }
                    ++loc5;
                }
            }
            else if (this._consumeEverydayIcon) 
            {
                this.consumeEverydayIcon.hide();
                this.consumeEverydayIcon.setFlashEffect(false);
                if (this._consumeEverydayModule && !this._consumeEverydayModule.isHide) 
                {
                    this._consumeEverydayModule.hide();
                }
            }
            return;
        }

        /* internal  */onConsumeEverydayGetRewardHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.getConsumeYBRetReward(loc1);
            return;
        }

        /* internal  */onConsumeEverydayIconClickHandler(arg1: DataEvent): void
        {
            if (this.consumeEverydayModule.isHide) 
            {
                this.consumeEverydayModule.show();
                this.consumeEverydayModule.updateModule();
            }
            else 
            {
                this.consumeEverydayModule.hide();
            }
            this.consumeEverydayIcon.setFlashEffect(false);
            return;
        }

        private /* var */_consumeCount: IConsumeCountGiftBagModule;

        private /* var */_consumeCountIcon: ConsumeCountIcon;

        private /* var */_consumeEverydayIcon: ConsumeEverydayIcon;

        private /* var */_consumeEverydayModule: IConsumeEverydayModule;

        private /* var */_isGetCodes: Boolean=false;

        private /* var */hadGetArr: Array<any>;
    }
